package app.auth;

import app.vo.User;
import io.javalin.core.security.RouteRole;
import io.javalin.http.Context;
import io.javalin.http.Handler;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
 * @author Sebastian Rodriguez, 2020. email: sebastian.rodriguez@rmit.edu.au
 */
public class AccessManager implements io.javalin.core.security.AccessManager {
    private static final String USER_KEY = "currentUser";


    public static User getSessionCurrentUser(Context ctx) {
        User user = ctx.sessionAttribute(USER_KEY);
        return user;
    }


    public static void loginUser(Context ctx, User user){
        ctx.sessionAttribute(USER_KEY,user);
    }

    public static void logout(Context ctx) {
        ctx.sessionAttribute(USER_KEY,null);
    }


    @Override
    public void manage(@NotNull Handler handler, @NotNull Context ctx, @NotNull Set<RouteRole> routeRoles) throws Exception {
        handler.handle(ctx);
    }
}
